package com.kotlin.mvpdemo.base;

import android.util.Log;

import androidx.arch.core.util.Function;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.Transformations;

import com.kotlin.mvpdemo.entity.ApiResponse;
import com.kotlin.mvpdemo.entity.BaseEntity;
import com.kotlin.mvpdemo.net.Api;
import com.kotlin.mvpdemo.net.ApiService;

/**
 * author : fang.zhang
 * e-mail : initzf@126.com
 * date   : 2020/6/6
 * desc   : 逻辑处理的父类、本来是用来处理逻辑的，但我不想再分出一层数据中心类，所以在逻辑处理类来请求网络数据，
 * 大家没意见吧！！！
 */
public abstract class BasePresenter<V extends BaseView> implements LifecycleObserver {
    private static final String TAG = "BasePresenter";
    private boolean isFinish = false;
    protected V mView;
    private LifecycleOwner owner;
    protected ApiService mApi;


    protected void onAttach(V view, LifecycleOwner owner) {
        this.mView = view;
        this.owner = owner;
        mApi = Api.request();

        owner.getLifecycle().addObserver(this);
    }


    /**
     * 处理统一的 livedata 数据
     *
     * @param source 数据源
     * @param t      业务所需要的数据
     * @param <X>
     * @param <T>
     * @return
     */
    protected <X, T> LiveData<T> exceute(LiveData<X> source, T t) {
        if (mView != null) {
            mView.loading();
        }
        return Transformations.map(source, (Function<X, T>) (Function<ApiResponse<BaseEntity<T>>, T>) input -> filterData(input));
    }

    /**
     * 过滤数据
     *
     * @param response 后台返回的json
     * @param <T>      业务所需要的数据
     * @return
     */
    private <T> T filterData(ApiResponse<BaseEntity<T>> response) {
        mView.dismiss();
        if (response.code == ApiResponse.ERROR_CODE) {
            mView.showError(response);
            return null;
        }
        BaseEntity<T> body = response.getBody();

        if (body.getErrorCode() == 0) {
            return body.getData();
        } else {
            mView.showError(new ApiResponse(body));
        }
        return null;
    }


    protected LifecycleOwner getOwner() {
        return owner;
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    protected void onCreate() {
        Log.i(TAG, "onCreate: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    protected void onResume() {
        Log.i(TAG, "onResume: ");
        isFinish = false;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    protected void onPause() {
        Log.i(TAG, "onPause: ");
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    protected void onStop() {
        Log.i(TAG, "onStop: ");
        mView.dismiss();
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    protected void onDestory() {
        isFinish = true;
        mView = null;
        owner.getLifecycle().removeObserver(this);
        mApi = null;
        owner = null;
        Log.i(TAG, "onDestory: ");
    }
}
